package com.bittech.chatroom.dao;

import com.alibaba.druid.pool.DruidPooledConnection;
import com.bittech.chatroom.entity.UserAccount;
import org.apache.commons.codec.digest.DigestUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author: yuisama
 * @Date: 2019-06-18 16:00
 * @Description:user_info dao
 */
public class AccountDao extends BaseDao {
    /**
     * 用户登录
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    public UserAccount userLogin(String userName,String password) {
        DruidPooledConnection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        UserAccount account = null;
        try {
            connection = getConnection(true);
            String sql = "select * from user_info where username = ? and password = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,userName);
            statement.setString(2, DigestUtils.md5Hex(password));
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                account = extractAccount(resultSet);
            }else {
                throw new IllegalArgumentException("用户信息校验失败，请检查用户名与密码");
            }
        }catch (Exception e) {
            System.err.println("用户登录失败");
            e.printStackTrace();
        }finally {
            closeResources(statement,resultSet,connection);
        }
        return account;
    }

    /**
     * 用户注册
     * @param account 要注册的用户实体
     * @return 是否注册成功
     */
    public boolean userRegister(UserAccount account) {
        DruidPooledConnection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        boolean isSuccess = false;
        try {
            if (account == null) {
                throw new IllegalArgumentException("要注册的用户实体为空!");
            }
            connection = getConnection(true);
            String sql = "insert into user_info(username, password) values (?,?)";
            statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            statement.setString(1,account.getUserName());
            statement.setString(2,DigestUtils.md5Hex(account.getPassword()));
            isSuccess = (statement.executeUpdate() == 1);
        }catch (Exception e) {
            System.err.println("用户注册失败!");
            e.printStackTrace();
        }finally {
            closeResources(statement,resultSet,connection);
        }
        return isSuccess;
    }

    private UserAccount extractAccount(ResultSet resultSet) throws SQLException {
        UserAccount userAccount = new UserAccount();
        userAccount.setId(resultSet.getInt("id"));
        userAccount.setUserName(resultSet.getString("username"));
        userAccount.setPassword(resultSet.getString("password"));
        userAccount.setPhotoUrl(resultSet.getString("photo"));
        return userAccount;
    }
}
